<template>
  <div id="GlobalHeader">
    <a-row :wrap="false">
      <a-col flex="200px"
        ><div class="title-bar">
          <img class="logo" src="../assets/logo.png" alt="" />
          <div class="title">全国最牛网站</div>
        </div></a-col
      >
      <a-col flex="auto" class="second"
        ><a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="doMenuClick"
        />
      </a-col>
      <a-col flex="80px">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
            {{ loginUserStore.loginUser.username ?? "无名" }}
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login" ghost>登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import { h, ref } from "vue";
import {
  HomeOutlined,
  MailOutlined,
  AppstoreOutlined,
  SettingOutlined,
} from "@ant-design/icons-vue";
import { MenuProps } from "ant-design-vue";
import { useRouter } from "vue-router";
import { userLoginUserStore } from "../store/userLoginUserStore";
const loginUserStore = userLoginUserStore();
const router = useRouter();

//路由跳转事件
const doMenuClick = ({ key }: { key: string }) => {
  router.push({ path: key });
};
//每次切换页面相应的，监听路由棉花，更新当前菜单选中状态
router.afterEach((to, from, failure) => {
  current.value = [to.path];
});
const current = ref<string[]>(["mail"]);
const items = ref<MenuProps["items"]>([
  {
    key: "/",
    icon: () => h(AppstoreOutlined),
    label: "主页",
    title: "主页",
  },
  {
    key: "/user/login",
    icon: () => h(AppstoreOutlined),
    label: "用户登录",
    title: "用户登录",
  },
  {
    key: "/user/manager",
    icon: () => h(AppstoreOutlined),
    label: "用户管理",
    title: "用户管理",
  },
  {
    key: "/user/register",
    icon: () => h(SettingOutlined),
    label: "用户注册",
    title: "用户注册",
  },
  {
    key: "others",
    label: h(
      "a",
      { href: "https://antdv.com", target: "_blank" },
      "管理员曹跃跃"
    ),
    title: "曹跃跃",
  },
]);
</script>
<style scoped>
#GlobalHeader {
  .title-bar {
    display: flex;
    align-items: center;
  }
  .title {
    color: black;
    font-size: 18px;
    margin-left: 16px;
  }
  .logo {
    height: 48px;
  }
}
</style>
